from google.appengine.ext import db
from math import sqrt

def validateLatitude(value):
	if value < -360 or value > 260:
		raise db.BadValueError('Latitude must be valid')
	return value

def validateLongitude(value):
	if value < -360 or value > 360:
		raise db.BadValueError('Longitude must be valid')
	return value	

class Point(db.Model):
	lt = db.FloatProperty(verbose_name="Latitude",required=True,validator=validateLatitude)
	lg = db.FloatProperty(verbose_name="Longitude",required=True,validator=validateLongitude)
	description = db.StringProperty(verbose_name="Description",required=True, multiline=True)
	point_type = db.StringProperty(default='Radar',choices=['Radar', 'Police Operation', 'Mobile Radar'])
	created_on = db.DateTimeProperty(auto_now_add=True)
	created_by = db.UserProperty(auto_current_user_add=True)


	def __str__(self):
		return '%s , %s - %s' % (self.lt,self.lg,self.description)

	def get_position_string(self):
		return '%s,%s' % (self.lt,self.lg)
	
	def get_lt(self):
		return '%s' % self.lt

	def get_lg(self):
		return '%s' % self.lg
				
	def get_creator(self):
		return self.created_by
	
	def get_creation_time(self):
		return self.created_on

	def get_type(self):
		return '%s' % self.point_type
	
	def distdeg(self, lt, lg):
		return sqrt((self.lt - lt)**2 + (self.lg - lg)**2)

	def js(self):
		return 'new GLatLng(%f,%f)' % (self.lt, self.lg)
		
	def get_description(self):
		return '%s' % self.description
	